const THREE=require('three')  //npm install three --save
// import * as THREE from 'three'

// lon1经度 lat纬度
//lonlat 标记点
//lonlat1 坐标1
//lonlat2 坐标2
function azimuth(lonlat1, lonlat2) {
    var lon1=THREE.MathUtils.degToRad(lonlat1[1])
    var lat1=THREE.MathUtils.degToRad(lonlat1[0])
    var lon2=THREE.MathUtils.degToRad(lonlat2[1])
    var lat2=THREE.MathUtils.degToRad(lonlat2[0])

    if(lon1==lon2){
        if (lat2 >= lat1){
            return 0
        }else{
            return 180
        }
    }

    if (lat1 == lat2){
        if(lon2 > lon1 ){
            return 90
        }else if(lon2 < lon1){
            return 270
        }
    }
    var a = THREE.Math.radToDeg(Math.atan((lon2 - lon1) * Math.cos(lat2) / (lat2 - lat1)))
    if(lat2 > lat1){

        if(lon2 > lon1){
            return a
        }else{
            return a + 360
        }

    }else{
        return a + 180
    }
}

/*
由于引入three时有可能会出现一些问题，
因此将Threejs中弧度与角度转化工具，手动写了出来
 */
function azimuth2(lonlat1, lonlat2) {
    var lon1=lonlat1[1]*(Math.PI/180)
    var lat1=lonlat1[0]*(Math.PI/180)
    var lon2=lonlat2[1]*(Math.PI/180)
    var lat2=lonlat2[0]*(Math.PI/180)

    if(lon1==lon2){
        if (lat2 >= lat1){
            return 0
        }else{
            return 180
        }
    }

    if (lat1 == lat2){
        if(lon2 > lon1 ){
            return 90
        }else if(lon2 < lon1){
            return 270
        }
    }

    var a = Math.atan((lon2 - lon1) * Math.cos(lat2) / (lat2 - lat1))*(180/Math.PI)
    if(lat2 > lat1){

        if(lon2 > lon1){
            return a
        }else{
            return a + 360
        }

    }else{
        return a + 180
    }

}